// pages/distribution/myDistribution/myDistribution.js
const distributionService = require('../../../../api/distributionService.js')
const backCashService = require('../../../../api/backCashService.js')
const utils = require('../../../../utils/utils.js')

//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabType: ["本月", "本季", "本年"],
    tabSelected: 0,
    member: {
      nickName: '未获取到会员昵称'
    },
    rankList: [{}, {}, {}]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let memberId = ''
    if (app.globalData.userInfo) {
      if (app.globalData.userInfo.member) {
        this.setData({
          member: app.globalData.userInfo.member
        })
        this.getMemberInfo()
        memberId = app.globalData.userInfo.member.id
        distributionService.queryGroup(memberId).then(res => {
          console.log(res)
        }).catch(err => {
          wx.showToast({
            title: err.message,
            icon: 'none',
            duration: 2000
          })
        })
        let date = new Date()
        let now = utils.formatTime(date).replace(/\//g, '-')
        console.log(date.getDate(), now)
        let startDate = now.slice(0, 8) + '01 00:00:00'
        let endDate = now
        this.queryRank(startDate, endDate)
      } else {
        wx.showToast({
          title: "您还不是会员，请绑定手机号成为会员~",
          icon: 'none',
          duration: 2000
        })
      }
    } else {
      wx.showToast({
        title: "您还没有登录哦，请点击头像授权登陆~",
        icon: 'none',
        duration: 2000
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //切换顶部标签
  switchTab: function (e) {
    this.setData({
      tabSelected: e.currentTarget.dataset.idx
    });
    //如果需要加载数据
    // console.log(e.currentTarget.dataset.idx)
  },
  //swiperChange
  bindChange: function (e) {
    var that = this;
    this.setData({
      tabSelected: e.detail.current
    });
    //如果需要加载数据
    console.log(e.detail.current)
    let date = new Date()
    let now = utils.formatTime(date).replace(/\//g, '-')
    console.log(date.getDate(), now)
    if (e.detail.current === 0) {
      let startDate = now.slice(0, 8) + '01 00:00:00'
      let endDate = now
      that.queryRank(startDate, endDate)
    } else if (e.detail.current === 1) {
      let nowMonth = date.getMonth()
      let month = utils.timeFormat(that.getQuarterStartMonth(nowMonth))
      let startDate = now.slice(0, 4) + '-' + month + '-01 00:00:00'
      let endDate = now
      that.queryRank(startDate, endDate)
    } else if (e.detail.current === 2) {
      let startDate = now.slice(0, 4) + '-01-01 00:00:00'
      let endDate = now
      that.queryRank(startDate, endDate)
    } else {
      let startDate = now.slice(0, 8) + '01 00:00:00'
      let endDate = now
      that.queryRank(startDate, endDate)
    }
  },
  // 查询本季度开始时间
  getQuarterStartMonth: function (nowMonth) {
    var quarterStartMonth = 0;
    if (nowMonth < 3) {
      quarterStartMonth = 0;
    }
    if (2 < nowMonth && nowMonth < 6) {
      quarterStartMonth = 3;
    }
    if (5 < nowMonth && nowMonth < 9) {
      quarterStartMonth = 6;
    }
    if (nowMonth > 8) {
      quarterStartMonth = 9;
    }
    return quarterStartMonth;
  },
  // 获取提现账户详情信息
  getMemberInfo() {
    backCashService.queryBackCashAccountDetails().then(res => {
      console.log(res)
      let member = this.data.member
      member = {
        ...member,
        backCashTotal: res.backCashTotal.toFixed(2),
        canBackCash: res.canBackCash.toFixed(2),
        hasBackCash: res.hasBackCash.toFixed(2),
        stayBackCash: res.stayBackCash.toFixed(2),
      }
      this.setData({
        member: member
      })
      console.log(member)
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000
      })
    })
  },
  // 查询榜单
  queryRank: function (startDate, endDate) {
    distributionService.queryRank(this.data.member.id, 1, 3, startDate, endDate, false).then(res => {
      console.log(res)
      let tempList = []
      if (res & res.records.length > 0) {
        for (let i = 0; i < 3; i++) {
          const ele = res.records[i];
          if (ele) {
            tempList.push(item)
          } else {
            tempList.push({})
          }
        }
      } else {
        tempList = [{}, {}, {}]
      }
      this.setData({
        rankList: tempList
      })
    }).catch(err => {
      console.log(err)
      wx.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000
      })
    })
  },
  // 前往收益明细
  toProfit: function () {
    wx.navigateTo({
      url: '../profit/profit',
    })
  },
  toMyGroup: function () {
    wx.navigateTo({
      url: '../teamList/teamList',
    })
  },
  // 前往龙虎榜
  toRankingList: function () {
    wx.navigateTo({
      url: '../rankingList/rankingList',
    })
  },
  toInvitation: function () {
    wx.navigateTo({
      url: '../../invite/invite',
    })
  },
})